<template>
  <div class="home">
    <div class="top">
      <div class="top-left">
        <div class="logo">
          <svg class="icon"
               aria-hidden="true">
            <use xlink:href="#icon-zufang"></use>
          </svg>
        </div>
        <h3>租房管理系统</h3>
      </div>
      <div class="top-right">
        <el-avatar size="medium"
                   :src="circleUrl"></el-avatar>
         <p class="welcome">
            欢迎您，{{role}}
         </p>
          <el-button class="login-out-btn"  @click="loginout()">退出</el-button>
      </div>
    </div>
    <div class="container">
      <el-aside width="220px">
        <el-menu :default-active="String(activeNav)"
                 class="el-menu-vertical-demo"
                 @open="handleOpen"
                 background-color="#222d32"
                 text-color="#f4f5f5"
                 @close="handleClose"
                 :unique-opened="true"
                 router>
          <el-submenu v-if="item.children"
                      :index="item.path"
                      v-for="item in menuData"
                      :key="item.key">
            <template slot="title">
              <i :class="item.icon"
                 class="iconfont"></i>
              <span>{{item.name}}</span>
            </template>
            <el-menu-item :index="subItem.path"
                          v-for="subItem in item.children"
                          :key="subItem.key">
              <i :class="subItem.icon"
                 class="iconfont"></i>
              {{subItem.name}}</el-menu-item>
          </el-submenu>
          <el-menu-item v-else
                        :index="item.path"
                        :key="item.name">
            <i :class="item.icon"
               class="iconfont"></i>
            <span slot="title">{{item.name}}</span>
          </el-menu-item>
        </el-menu>
      </el-aside>
      <div class="context-box">
        <!--欢迎使用本系统-->
        <pointout></pointout>
        <TabMenu></TabMenu>
      </div>
    </div>
  </div>
</template>
<script lang="js">
 import pointout from '@/components/pointout.vue'
 import TabMenu from '@/components/TabMenu.vue'
  export default{
    components: {
      pointout,
      TabMenu
    },
    data(){
     return {
      circleUrl: "https://fuss10.elemecdn.com/e/5d/4a731a90594a4af544c0c25941171jpeg.jpeg",
      menuData:[],
      role: ''
     }
    },
    computed: {
      activeNav() {
            // 获取当前激活的导航
            return this.$route.path;
        }
    },
    mounted(){
      sessionStorage.removeItem("openTab");

      this.getMenuData()
    },
    methods:{
      getMenuData() {
        let data =this.$store.state.userInfo;
        this.role = data.username;
        let url = '';
        if(data.role === 'admin'){
           url = './static/adminMenu.json'
        } else {
          url = './static/rentMenu.json'
        }
      this.$axios.get(url)
        .then(res => {
          this.menuData = res.data.menuData
        })
        .catch(error => {
          console.log(error)
        })
    },
      handleOpen(key, keyPath) {
        console.log("12313");
        console.log(key, keyPath);
      },
      handleClose(key, keyPath) {
        console.log(key, keyPath);
      },
      // 退出登录
      loginout(){
          this.$confirm('确定要退出登录吗?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
      }).then(() => {
        this.$message({
            type: 'success',
            message: '退出成功!'
      });
        this.$router.push ({
           path: '/login'
          });
      }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          });
        });
      }
    }
  }
</script>
<style lang="scss" scoped>
.home {
  font-size: 20px;
  height: 100%;
  .top {
    display: flex;
    justify-content: space-between;
    align-items: center;
    height: 70px;
    padding: 0 20px;
    background: #3c8dbc;
    color: #fff;
    .top-left {
      display: flex;
      align-items: center;
      .logo {
        width: 50px;
        height: 50px;
        border-radius: 50%;
        background-color: #fff;
        display: flex;
        justify-content: center;
        align-items: center;
      }
      h3 {
        margin-left: 10px;
        margin-bottom: 0px;
      }
    }
    .top-right {
      display: flex;
      align-items: center;
      .welcome {
          margin-left: 12px;
          font-size: 20px;
      }
     .login-out-btn {
      background: transparent;
      border: none;
      outline: none;
      color: #fff;
      font-size: 20px;
     }
    }
  }
  .container {
    display: flex;
    height: calc(100vh - 70px);
    .el-menu {
      height: 100%;
      .el-submenu__title {
        span {
          font-size: 18px;
        }
      }
      .el-submenu {
        .el-submenu__title {
          font-size: 18px;
        }
        .el-menu-item {
          font-size: 18px;
        }
      }

      .el-dropdown-menu__item,
      .el-menu-item {
        font-size: 18px;
      }
      .iconfont {
        color: #fff;
        font-size: 16px;
        margin-right: 10px;
      }
    }
    .context-box {
      width: calc(100vw - 220px);
    }
  }
}
</style>
